package com.parsechina.hpush.broker.parser;

import io.netty.handler.codec.mqtt.MqttSubscribeMessage;
import io.netty.handler.codec.mqtt.MqttTopicSubscription;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linfeng
 * @since 2016/11/22
 */
public class SubscribeMessage extends PushMessage {
    private static final long serialVersionUID = -8236074301512114405L;

    private MqttSubscribeMessage subscribeMessage;
    private int messageId;

    public SubscribeMessage(MqttSubscribeMessage msg) {
        super(msg);
        this.subscribeMessage = msg;
        this.messageId = msg.variableHeader().messageId();
    }

    public SubscribeMessage() {
    }

    public int getMessageId() {
        return messageId;
    }

    private List<MqttTopicSubscription> m_subscriptions = new ArrayList<>();

    public List<MqttTopicSubscription> subscriptions() {
        if (subscribeMessage == null) {
            return m_subscriptions;
        }
        return subscribeMessage.payload().topicSubscriptions();
    }

    public void addSubscription(MqttTopicSubscription subscription) {
        m_subscriptions.add(subscription);
    }

}
